package com.antexpress.controller;

import com.antexpress.bean.Authentication;
import com.antexpress.common.Const;
import com.antexpress.common.Response;
import com.antexpress.pojo.Employee;
import com.antexpress.service.CourierService;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/courier/")
public class CourierController {
    @Autowired
    private CourierService courService;
    
    @RequestMapping(value="list_courier.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listCourier(String province,String city,String district,String town,int pageNo, HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        resp = courService.courierShowOnPage(province,city,district,town,pageNo, 10);
        return resp;
    }
    
    @RequestMapping(value="list_all_courier.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listAllCourier(HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp!=null && Authentication.isHRAdmin(currentEmp)){  //登录，且拥有HR管理员权限
            resp = courService.getCourierList();
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="change_courier.do", method=RequestMethod.POST)
    @ResponseBody
    public Response changeCourier(String courierId,String townId,HttpSession session){
        
        System.out.println("*********************** townID "+ townId);
        System.out.println("*********************** courierID "+ courierId);
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp!=null && Authentication.isHRAdmin(currentEmp)){  //登录，且拥有HR管理员权限
            resp = courService.changeCourier(townId,courierId);
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="delete_courier.do", method=RequestMethod.POST)
    @ResponseBody
    public Response deleteCourier(String id,HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp!=null && Authentication.isHRAdmin(currentEmp)){  //登录，且拥有HR管理员权限
            resp = courService.deleteCourier(id);
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="batch_delete_courier.do", method=RequestMethod.POST)
    @ResponseBody
    public Response deleteChosenCourier(String[] idArray,HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp!=null && Authentication.isHRAdmin(currentEmp)){  //登录，且拥有HR管理员权限
            for(String id:idArray){
                resp = courService.deleteCourier(id);
            }
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
}
